package com.hongpu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hongpu.config.SecurityContextUtil;
import com.hongpu.domain.CoolTowerEmployee;
import com.hongpu.domain.CoolTowerEmployeeSalaryProjectRelationship;
import com.hongpu.dto.CoolTowerEmployeeDto;
import com.hongpu.enums.OnDutyStatusEnum;
import com.hongpu.mapper.CoolTowerEmployeeMapper;
import com.hongpu.service.CoolTowerEmployeeSalaryProjectRelationshipService;
import com.hongpu.service.CoolTowerEmployeeService;
import com.hongpu.service.MultiTenantService;
import com.hongpu.utils.ResultDto;
import com.hongpu.vo.SelectOptionVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Michael zhang
 * @description 针对表【cool_tower_employee(冷却塔安装人员管理)】的数据库操作Service实现
 * @createDate 2025-06-10 15:50:16
 */
@Service
public class CoolTowerEmployeeServiceImpl extends ServiceImpl<CoolTowerEmployeeMapper, CoolTowerEmployee>
        implements CoolTowerEmployeeService {
    @Autowired
    CoolTowerEmployeeMapper baseMapper;
    @Autowired
    MultiTenantService multiTenantService;
    @Autowired
    CoolTowerEmployeeSalaryProjectRelationshipService coolTowerEmployeeSalaryProjectRelationshipService;

    /**
     * 新增
     *
     * @param coolTowerEmployee
     */
    @Override
    public ResultDto add(CoolTowerEmployee coolTowerEmployee) {
        try {
            boolean add = save(coolTowerEmployee);
            return add ? ResultDto.ok() : ResultDto.fail();
        } catch (Exception e) {
            return ResultDto.fail(e.getLocalizedMessage());
        }
    }

    /**
     * 修改
     *
     * @param coolTowerEmployee
     */
    @Override
    public ResultDto update(CoolTowerEmployee coolTowerEmployee) {
        try {
            boolean update = updateById(coolTowerEmployee);
            return update ? ResultDto.ok() : ResultDto.fail();
        } catch (Exception e) {
            return ResultDto.fail(e.getLocalizedMessage());
        }
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public ResultDto delete(Long id) {
        try {
            // 删除用户前，首先判断该用户是否在cool_tower_employee_salary_project_relationship中
            QueryWrapper<CoolTowerEmployeeSalaryProjectRelationship> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("cool_tower_employee_id", id);
            List<CoolTowerEmployeeSalaryProjectRelationship> list = coolTowerEmployeeSalaryProjectRelationshipService.list(queryWrapper);
            if (list.size() > 0) {
                return ResultDto.fail("该用户正在使用，不能删除");
            }
            boolean delete = removeById(id);
            return delete ? ResultDto.ok() : ResultDto.fail();
        } catch (Exception e) {
            return ResultDto.fail(e.getLocalizedMessage());
        }
    }

    /**
     * 启用
     *
     * @param id
     */
    @Override
    public ResultDto active(Long id) {
        try {
            boolean active = baseMapper.active(id, SecurityContextUtil.getUserId());
            return active ? ResultDto.ok() : ResultDto.fail();
        } catch (Exception e) {
            return ResultDto.fail(e.getLocalizedMessage());
        }
    }

    /**
     * 分页
     *
     * @param coolTowerEmployeeDto
     */
    @Override
    public ResultDto page(CoolTowerEmployeeDto coolTowerEmployeeDto) {
        try {
            IPage<CoolTowerEmployee> page = new Page<>(coolTowerEmployeeDto.getCurrent(), coolTowerEmployeeDto.getSize());
            IPage<CoolTowerEmployee> selectPage = baseMapper.page(page, coolTowerEmployeeDto);
            return ResultDto.ok(selectPage);
        } catch (Exception e) {
            return ResultDto.fail(e.getLocalizedMessage());
        }
    }

    /**
     * 获取人员编号 : slug（2位） + 编号（6位） 共计  8位
     *
     * @return String
     */
    @Override
    public ResultDto getWorkNum() {
        String workNum = "";
        // 获取当前租户的slug
        String slug = multiTenantService.getSlugByTenantId(SecurityContextUtil.getTenantId());
        // 查询数据库中是否已经存在工号
        String isExistsWorkNum = baseMapper.isExistsWorkNum();
        if (StringUtils.isEmpty(isExistsWorkNum)) {
            // 数据库中没有存在工号,  则从1开始
            workNum = slug + String.format("%06d", 1);
        } else {
            // 数据库中存在工号, 则从数据库中最大工号+1开始
            workNum = baseMapper.getNewWorkNum();
        }
        return ResultDto.ok(workNum);
    }

    /**
     * 获取在职状态
     */
    @Override
    public ResultDto getOnDutyStatus() {
        try {
           List<SelectOptionVo<String>> selectOptionVos = new ArrayList<>();
            OnDutyStatusEnum[] values = OnDutyStatusEnum.values();
            for (OnDutyStatusEnum value : values) {
                SelectOptionVo<String> selectOptionVo = new SelectOptionVo<>();
                selectOptionVo.setLabel(value.getMessage());
                selectOptionVo.setValue(value.getCode());
                selectOptionVos.add(selectOptionVo);
            }
           return ResultDto.ok(selectOptionVos);
        } catch (Exception e) {
            return ResultDto.fail(e.getLocalizedMessage());
        }
    }

    /**
     * 选择人员时用到的下拉列表
     */
    @Override
    public ResultDto selectEmployeeList() {
        try {
            List<SelectOptionVo<String>> selectOptionVos = new ArrayList<>();
            List<CoolTowerEmployee> list = list();
            for (CoolTowerEmployee employee : list) {
                SelectOptionVo<String> select = new SelectOptionVo<>();
                select.setLabel(employee.getName());
                select.setValue(employee.getId().toString());
                selectOptionVos.add(select);
            }
            return ResultDto.ok(selectOptionVos);
        } catch (Exception e) {
            return ResultDto.fail(e.getLocalizedMessage());
        }
    }

    /**
     * 根据用户ID获取用户信息
     *
     * @param id
     */
    @Override
    public ResultDto getInfoById(Long id) {
        try {
            CoolTowerEmployee employee = getById(id);
            return ResultDto.ok(employee);
        } catch (Exception e) {
            return ResultDto.fail(e.getLocalizedMessage());
        }
    }

    /**
     * 查询用户打过卡的项目
     *
     * @param id
     */
    @Override
    public ResultDto getProjectList(Long id) {
        return null;
    }
}




